/**
 * Created by wenchuan on 2018/5/9.
 */
define(function () {

    var self,
        app = win.app,
        catalogs = [],
        hasCatalogs = [],//存放已经请求过的类型列表
        searchData = {
            str: '',
            GWAH001: '',
            GWAM004: '', //产品系列
            types: []
        },
        pageUi,
        pageParameters;

    var Classes = app.Module.extend({

        init: function () {
            self = this;
            pageParameters = app.pageParameters['classes_page'];
            self._super({}, win);
            self.getTypes();
        },
        getTypes: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getGwahData",
                cb: function (result) {
                    if (result.ok) {
                        var items = result.data.data;
                        if (pageParameters && pageParameters.type) {
                            searchData.GWAH001 = pageParameters.type;
                        } else {
                            searchData.GWAH001 = items[0].GWAH001;
                        }

                        searchData.types.pushArray(items);
                        self.getCatalogs();
                    }
                }
            });
        },
        getCatalogs: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getGwaoData",
                loading:true,
                xdata: {
                    GWAH001: searchData.GWAH001
                },
                cb: function (result) {
                    if (result.ok) {
                        var items = result.data.data;
                        catalogs.pushArray(items, true).addMap({
                            GWAH001: searchData.GWAH001
                        }, true);
                        if (hasCatalogs.indexOf(searchData.GWAH001) == -1) {
                            hasCatalogs.push(searchData.GWAH001);
                            items.forEach(function (v, i) {
                                var pDivId = 'content' + searchData.GWAH001 + i;
                                self._refreshIds.push(pDivId);
                                pageUi.right_tab.append('<div id="' + pDivId
                                    + '" class="mui-control-content mui-scroll-wrapper' + (i == 0 ? " mui-active" : '') + '"><div class="mui-scroll"><classesitem-template :model="items"></classesitem-template></div></div>');
                            });
                        }

                        if (items.length > 0) {
                            searchData.GWAM004 = items[0].GWAO001;
                        }
                        self._initRefreshById(win, 'content' + searchData.GWAH001 + '0');
                        $('#tab-content>div').removeClass('mui-active');
                        $('#' + 'content' + searchData.GWAH001 + '0').addClass('mui-active');
                        $('#segmentedControls>a').removeClass('mui-active');
                        $('#segmentedControls>a').first().addClass('mui-active');
                    }
                }
            });
        },
        _initRefresh: function () { // 实例化刷新组件
            this._super(win);
        },
        _refreshCb: function (goods) {// 刷新组件回调函授
            var _super = this._super;
            app.ajax({
                xurl: "api/wx/gwat100/getGwamPage",
                // xtime: '1d',
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit,
                    GWAM006: searchData.GWAH001,
                    GWAM004: searchData.GWAM004, //产品系列
                    str: searchData.str
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data;
                        data.data.addMap({
                            GWCM003: 1
                        });

                        _super({
                            obj: goods,
                            data: data.data,
                            nodata: '暂无商品数据...'
                        });
                    }
                }
            });
        },
        _lazy: function () {
            this._super();
            mui('#scrollleft').scroll({
                deceleration: 0.0005
            });
        },
        _ready: function () {
            Vue.component('classesitem-template', {
                template: app.getByid(doc, 'classesitem-template'),
                props: {
                    model: Array
                },
                methods: {
                    showDetail: function (item) {
                        app.openPage({
                            src: "gwcxd/classes-info.html",
                            argsKey: 'classesinfo_page',
                            args: {
                                item: app.cloneJSON(item),
                                last: pageParameters
                            }
                        });
                    }
                }
            });

            new Vue({
                el: app.getByid(doc, 'segmentedControls'),
                data: {
                    catalogs: catalogs
                },
                methods: {
                    showCatalogGoods: function (i, GWAH001) {
                        searchData.GWAM004 = GWAH001;
                        self._initRefreshById(win, 'content' + searchData.GWAH001 + i);
                    }
                }
            });

            new Vue({
                el: app.getByid(doc, 'searchDiv1'),
                data: searchData,
                methods: {
                    serchData: function () {
                        self._refresh();
                    },
                    typeChange: function () {
                        self.getCatalogs();
                    }
                }
            });

            if (!pageParameters) {

                new Vue({
                    el: doc.getElementById('footer'),
                    data: {
                        show: true
                    },
                    methods: {
                        gouwuche: function () {
                            app.openPage({
                                src: 'gwcxd/gwc.html',
                                argsKey: 'classes_page'
                            });
                        },
                        qianzaishangji: function () {
                            app.openPage({
                                src: 'gwcxd/qzsj.html'
                            });
                        }
                    }
                });
            }

            pageUi = {
                right_tab: $('#tab-content')
            };

        }
    });

    return new Classes();

});